<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../base.jsp" %>
<style>
	.content-wrapper{margin-left: 0px}
</style>
<script type="text/javascript">
$(function() {
	init();
});
function init(){
	$.ajax({
		type:"POST",
		url: path+'local/getNodesList',
		success: function(result){
			$("#nodeList").html("");
			if(result){
				var index = 0;
				for(var item in result.result){
					index  = parseInt(index)+1;
					if(index==1){
						showNodeInfo(item);
					}
					var appendStr = "<li class='active' id='"+index+"'><a href='#' onclick='toNodeIndex(1,\""+item+"\")'><i class='fa fa-server'></i>"+item+"</a></li>";
					$("#nodeList").append(appendStr);
				}
			}	
		}
	}); 
}
function showNodeInfo(ip){
	$("#nodeIp").val(ip);
	$.ajax({
		type:"POST",
		url: path+'local/getNodesList',
		success: function(result){
			$("#serviceTbody").html("");
			$("#addserviceTbody").html("");
			if(result){
				for(var item in result.result){
					var num = 0;
					var numObj=0;
					for(var i = 0;i<result.result[item].services.length;i++){
						for(var oItem in result.result[item].services[i]){
							numObj = parseInt(numObj)+1;
							if(result.result[item].services[i][oItem].state==1){
								num = parseInt(num)+1;
							}
						}
					}
				if(num>0){
					 var index = 1;
					 var appendStr = "<tr><td><a href='javascript:;'  onclick='toNodeIndex(1,\""+item+"\")'>"+result.result[item].hostname+"</a></td>";
					 appendStr+="<td><b>"+item+"</b></td>";
					 appendStr+="<td><a href='javascript:;' title='正常'><img id='u75_img'  src='../dist/img/u37.png'></a></td>";
					 appendStr+="<td>XXX</td><td>"+num+"/"+numObj+"</td><td>";
					 appendStr+="<a href='#' title='详情' onclick='toNodeIndex(1,\""+item+"\")'><i class='fa fa-info text-blue'></i>详情</a>";
					 appendStr+="<a href='#' title='删除' onclick='setServiceOfNode(\""+item+"\")'><i class='fa fa-remove text-red'></i>删除</a></td></tr>";
					 $("#serviceTbody").append(appendStr);	
				}else{
					var index = 1;
					var appendStr = "<tr><td><a href='javascript:;'  onclick='toNodeIndex(1,\""+item+"\")'>"+result.result[item].hostname+"</a></td>";
					 appendStr+="<td><b>"+item+"</b></td>";
					 appendStr+="<td><a href='javascript:;' title='未添加'><img id='u75_img'  src='../dist/img/u131.png'></a></td>";
					 appendStr+="<td>XXX</td><td>"+num+"/"+numObj+"</td><td>";
					 appendStr+="<a href='#' title='删除' onclick='deleteFreeNode(\""+item+"\")'><i class='fa fa-remove text-red'></i>删除</a></td></tr>";
					 $("#addserviceTbody").append(appendStr);	
				}
			}	
		}
	}
}); 
}

function deleteFreeNode(item){
	if(confirm('确实要进行此操作吗?')){
		$.ajax({
			cache:false,
			async:false,
			type:"POST",
			url: path+'local/delFreeNode',
			data:{ip:item},
			success: function(result){
				alert("操作成功");
				init();
			}
		});
	}
}

function setServiceOfNode(ip){
	if(confirm('确实要进行此操作吗?')){
	$.ajax({
		cache:false,
		async:false,
		type:"POST",
		url: path+'local/getNodesList',
		success: function(result){
			if(result){
				for(var item in result.result){
					if(item==ip){
						for(var i = 0;i<result.result[item].services.length;i++){
							for(var obj in result.result[item].services[i]){
								if(result.result[item].services[i][obj].state==1){
									$.ajax({
										cache:false,
										async:false,
										type:"POST",
										url: path+'cluster/updateState',
										data:{serviceName:obj,ip:ip,state:0},
										success: function(result){
											 if(result){
													
											 }
										}
									}); 
								}
							}
						}
					}
				}
				alert("操作成功");
				showNodeInfo(ip);
			}
		}
	});
	}
}
</script>
<body class="hold-transition skin-blue sidebar-mini">
	<div class="wrapper">
		<!-- Content Wrapper. Contains page content -->
		<div class="content-wrapper">
			<!-- Content Header (Page header) -->
			<section class="content-header">
				<h1>主机配置<small>节点管理</small></h1>
				<ol class="breadcrumb">
					<li><a href="javascript:;"><i class="fa fa-dashboard"></i> 首页</a></li>
					<li>主机</li>
					<li class="active">主机配置</li>
				</ol>
			</section>
			<!-- Main content -->
			<section class="content">
				<div class="row">
        			<div class="col-md-3">
        				<div class="box box-solid">
				          <div class="box-header with-border">
				            <h3 class="box-title">概览</h3>
				            <div class="box-tools">
				              <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
				              </button>
				            </div>
				          </div>
				          <div class="box-body no-padding">
				            <ul class="nav nav-pills nav-stacked"  id="nodeList">
				              
				            </ul>
				          </div>
				          <!-- /.box-body -->
				        </div>
        			</div>
        			<div class="col-md-9" id="local-list">
        				<div id="jspInclude">
        					<jsp:include page="local_list.jsp"></jsp:include>
        				</div>
						<div class="nav-tabs-custom" id="nav-tabs-custom" style="display: none;">
							<ul class="nav nav-tabs">
								<li class="active"><a href="javascript:;" data-toggle="tab">主机详情</a></li>
								<li><a href="javascript:;" data-toggle="tab">服务详情</a><input type="hidden" id="nodeIp" /></li>
							</ul>
							<div class="tab-content" id="serverInfo">
							</div>
						</div>
        			</div>
        		</div>
			</section>
		</div>
	</div>
	<script type="text/javascript">
	$(function () {
		$('.nav-stacked li').on('click',function(){
			$('.nav-stacked li').removeClass('active');
			$(this).addClass('active');
			var id = $(this).attr('id');
			linkByDom('serverInfo','${url }local/info?localId='+id);
			$('#jspInclude').remove();
			$('#nav-tabs-custom').show();
		});
		
		$('.nav-tabs li').on('click',function(){
			var id = $('#localId').val();
			$('.nav-tabs li').removeClass('active');
			$(this).addClass('active');
			var index = $(this).index();
			if(index == 0){
				linkByDom('serverInfo','${url }local/info?localId='+id+'&nodeIp='+$("#nodeIp").val());
			}else{
				linkByDom('serverInfo','${url }local/serverInfo?localId='+id+'&nodeIp='+$("#nodeIp").val());
			}
			$('#jspInclude').remove();
			$('#nav-tabs-custom').show();
		});
	});
	
	</script>
</body>